/*
* Copyright @2009-2012 www.happyelements.com, all rights reserved.
* Create date: 2012-7-30
* Piggy Dong
* 董文弈
* wenyi.dong@happyelements.com
*/
package com.happyelements.components
{
	import com.bit101.components.VSlider;

	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author dongpiggyi
	 */
	public class List extends Sprite
	{
		private var _windowWidth:Number;

		private var _windowHeight:Number;

		private var _itemContainer:Sprite;

		private const PADDING:Number = 5;
		
		private const GAP:Number = 100;

		public function List(width:Number = 100, height:Number = 300)
		{
			_windowWidth = width;
			_windowHeight = height;

			init();
		}

		private function init():void
		{
			_itemContainer = new Sprite();
			_itemContainer.x = PADDING;
			_itemContainer.y = PADDING;
			addChild(_itemContainer);

			var scrollBar:VSlider = new VSlider(this, _windowWidth - 3 * PADDING, PADDING, onScroll);
			scrollBar.width = 2 * PADDING;
			scrollBar.height = _windowHeight - 2 * PADDING;
			scrollBar.value = 100;

			var mask:Sprite = new Sprite();
			mask.graphics.beginFill(0x000000);
			mask.graphics.drawRect(PADDING, PADDING, _windowWidth - 2 * PADDING, _windowHeight - 2 * PADDING);
			mask.graphics.endFill();
			addChild(mask);

			_itemContainer.mask = mask;

			graphics.lineStyle(1, 0x000000);
			graphics.beginFill(0xFFFFFF);
			graphics.drawRect(0, 0, _windowWidth, _windowHeight);
			graphics.endFill();
		}

		private function onScroll(event:Event):void
		{
			var containerY:Number = 100 - (event.target as VSlider).value;
			containerY = (_windowHeight - _itemContainer.height) * containerY / 100;
			_itemContainer.y = containerY;
		}

		public function addItem(item:Sprite):void
		{
			if (item.width > item.height)
			{
				item.width = _windowWidth - 3 * PADDING;
				item.scaleY = item.scaleX;
			}
			else 
			{
				item.height = GAP;
				item.scaleX = item.scaleY;
			}
			
			item.x = (_windowWidth - 3 * PADDING) / 2;
			item.y = _itemContainer.numChildren * GAP + GAP / 2;
			item.buttonMode = true;

			_itemContainer.addChild(item);
		}
	}
}
